package com.gko100.action;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Category;
import com.gko100.model.Language;
import com.gko100.vo.CategoryVo;


public class CategoryAction extends AdminBaseAction {
	
	//action传过来的参数
	private String param;
	private String param_cateId;
	public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
	public String getParam_cateId() {
		return param_cateId;
	}
	public void setParam_cateId(String paramCateId) {
		param_cateId = paramCateId;
	}


	private Category cate;

	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	
	private List<Category> cateList;
	
	public List<Category> getCateList() {
		return cateList;
	}
	public void setCateList(List<Category> cateList) {
		this.cateList = cateList;
	}
	
	private Map<Category, List<Category>> cateMap;

	public Map<Category, List<Category>> getCateMap() {
		return cateMap;
	}
	public void setCateMap(Map<Category, List<Category>> cateMap) {
		this.cateMap = cateMap;
	}
	
	
	
	
	//一级菜单列表（会员管理、系统管理、网站管理、留言信息）
	public String find_cate() throws Exception {
		
		List<Category> cateList = adminManager.findCateListByCatePid(level_1);
		
		if(cateList!=null){
			this.setCateList(cateList);
		}
		return "cate_find_success";
		
	}
	
	//二级菜单列表右侧（所有）
	public String find_cate_left() throws Exception {
		
		System.out.println("-------find_cate_left-----------");
		if(this.getCate().getCateId()!=null){
			
			System.out.println("this.getCate().getCateId()-----"+this.getCate().getCateId());
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			if(cate!=null){
				this.setCate(cate);			
			}
			
			List<Category> cateList = adminManager.findCateListByCatePid(this.getCate().getCateId());
			
			if(cateList!=null&&cateList.size()>=1){
				this.setCateList(cateList);
			}else{
				this.setCateList(null);
			}
			
		}
		return "cate_left_find_success";
			
		
	}
	
	//网站管理三级菜单（中文，英文下内容） 
	public String find_cateListLev3() throws Exception {
		
		System.out.println("-----------find_cateListLev3------------");
		if(this.getParam()!=null){
			
			int cateId = Integer.parseInt(this.getParam());
			Category cate = adminManager.findCateByCateId(cateId);
			List<Category> cateList = adminManager.findCateListByCatePid(cateId);
			if(cate!=null){
				this.setCate(cate);
			}
			if(cateList!=null&&cateList.size()>=1){
				
				Map<Category, List<Category>> cMap = new LinkedHashMap<Category, List<Category>>();
				List<Category> cList = new ArrayList<Category>();
				if(cateList!=null&&cateList.size()>=1){
					System.out.println("-----------cateList------------");
					for(int i=0;i<cateList.size();i++){
						
						cList = adminManager.findCateListByCatePid(cateList.get(i).getCateId());
						if(cList!=null&&cList.size()>=1){
							cMap.put(cateList.get(i), cList);
						}else{
							cMap.put(cateList.get(i), null);
						}
						
					}
				}
				
				if(cMap!=null&&cMap.size()>=1){
					this.setCateMap(cMap);
				}
				
			}
			
			
		}
		
		return "cateListLev3_find_success";
	}
	
	
	//系统模块管理
	private Map<Category, List<Category>> systemCateMap;
	
	public Map<Category, List<Category>> getSystemCateMap() {
		return systemCateMap;
	}
	public void setSystemCateMap(Map<Category, List<Category>> systemCateMap) {
		this.systemCateMap = systemCateMap;
	}
	public String find_cateSystem() throws Exception {
		if(this.getSystemCateMap()!=null){
			this.setSystemCateMap(null);
		}
		Map<Category, List<Category>> cMap = new LinkedHashMap<Category, List<Category>>();
		List<Category> cateList = adminManager.findCateListByCatePid(0);
		
		if(cateList!=null&&cateList.size()>=1){
			for(int i=0;i<cateList.size();i++){
				
				
				List<Category> cList = adminManager.findCateListByCatePid(cateList.get(i).getCateId());
				if(cList!=null){
					System.out.println("cList.size()-----------"+cList.size());
					cMap.put(cateList.get(i), cList);
				}else{
					cMap.put(cateList.get(i), null);
				}
			}
		}
		this.setSystemCateMap(cMap);
		return "cateSystem_find_success";
	}
	
	//系统模块---网站管理----3级菜单(信息管理、信息添加)
	public String find_cateSystemLev3() throws Exception {

		if(this.getCateMap()!=null){
			this.setCateMap(null);
		}
		if(this.getParam()!=null){

			int cateId = Integer.parseInt(this.getParam());
			Category cate = adminManager.findCateByCateId(cateId);
			if(cate!=null){
				List<Category> cateList = adminManager.findCateListByCatePid(cateId);
				if(cateList!=null&&cateList.size()>=1){
					this.setCateList(cateList);
					Map<Category, List<Category>> cMap = new LinkedHashMap<Category, List<Category>>();
					for(int i=0;i<cateList.size();i++){
						//不能new一个cList(测试)
						List<Category> cList = adminManager.findCateListByCatePid(cateList.get(i).getCateId());
						if(cList!=null){
							cMap.put(cateList.get(i), cList);
						}else{
							cMap.put(cateList.get(i), null);
						}
					
					}
					this.setCateMap(cMap);
					
				}
				
			}
		}

		return "cateSystemLev3_find_success";
	}
	
	//添加子模块
	
	public String insert_cate() throws Exception{
		
		if(this.getCate().getCateId()!=null){
			//获得当前分类
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			this.setCate(cate);
		}
		
		
		return "cate_insert_success";
	}
	
	
	public String add_cate() throws Exception {
		
		//通过隐藏域吧catePid传过来
		if(this.getCate().getCatePid()!=null){
			Category c = adminManager.findCateByCateId(this.getCate().getCatePid());
			Category cate = new Category();
			cate.setName(this.getCate().getName());
			cate.setUrl(this.getCate().getUrl());
			cate.setCatePid(this.getCate().getCatePid());
			cate.setCateNote(this.getCate().getCateNote());
			cate.setLang(c.getLang());
			adminManager.add_cate(cate);
			return "cate_add_success";
			
		}else{
			
			return "back_error";
		}
	}
	
	public String del_cate() throws Exception{
		
		
		if(this.getCate().getCatePid()!=null){
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			adminManager.del_cate(cate);
			return "cate_del_success";
		}else{
			
			return "back_error";
		}

	}
	
	
	public String upload_cate() throws Exception {
		
		if(this.getCate().getCateId()!=null){
			
			Category cate = adminManager.findCateByCateId(this.getCate().getCateId());
			
			this.setCate(cate);
			return "cate_upload_success";
		}else{
			
			return "back_error";
		}
		
	}
	public String modify_cate() throws Exception {
		
		if(this.getCate().getCateId()!=null){
			
			adminManager.modify_cate(this.getCate());
			return "cate_modify_success";
		}else{
			
			return "back_error";
		}
	}
	
	public String find_cateNavList() throws Exception {
		
		Integer cateId = 3;

		Category cate = adminManager.findCateByCateId(cateId);
		if(cate!=null){
			this.setCate(cate);
			List<Category> cateList = adminManager.findCateListByCatePid(cate.getCateId());
			if(cateList!=null){
				this.setCateList(cateList);
			}
		}
		
		

		return "cateNavList_find_success";
	}
	
	
	
	
	
	

}
